var localData = {}
function insinfodetail(id){
	//var id = 168;
	$.ajax({
		//"url" : "http://127.0.0.1/app/ins/lookInsinfoDetail",
		"url" : Server + "/app/ins/lookInsinfoDetail",
		"type" : "Post",
		"dataType" : "Json",
		"data" : {
			id:id
		},
		success : function(data) {
			$("#download").click(function(){
				$(location).attr('href', 'insKnowledge12?id='+ data.id +'');
			})
			var total = (data.total == null)?0:data.total;
			var unit = (data.unit == null)?'元/天':data.unit;
			$("#name0").html(data.chineseName);
			$("#name1").html(data.chineseName);
			$("#name2").html(data.englishName);
			$("#model").html(data.serial);
			$("#make").html(data.make);
			$("#price").html(total+unit)
			$("#choosePrice").html(total+unit)
			$("#detail").html(data.insDesc)
			$("#test1 img").attr("src",data.infoSrc)
			if(data.instype.length!=0){
				var h = ""
				for(var m = 0;m<data.instype.length;m++){
					if(m==data.instype.length-1){
						h += data.instype[m].name;
					}else{
						h += data.instype[m].name + "、";
					}
				}
				$("#type").html(h);
			}
			if(data.infopro.length!=0){
				var h = ""
				for(var m = 0;m<data.infopro.length;m++){
					if(m==data.infopro.length-1){
						h += data.infopro[m].projectName;
					}else{
						h += data.infopro[m].projectName + "、";
					}
				}
				$("#all").html(h);
			}
			$.ajax({
				//"url" : "http://127.0.0.1/app/ins/lookInsInfoComment",
				"url" : Server + "/app/ins/lookInsInfoComment",
				"type" : "Post",
				"dataType" : "Json",
				"data" : {
					inId:id
				},
				success : function(data) {
					var i = 0;
					var thm = "";
					var userName = '';
					for(var n = 0;n<data.length;n++){
						var sm = '<li>\
								<div class="fontInfo">\
									<div class="left">#1</div>\
									<div class="right">#2</div>\
								</div>\
							</li>';
						userName = (data[n].username == null)?'匿名':data[n].username;
						thm += sm
							.replace("#1", data[n].content)
							.replace("#2", userName),
						i++;
					}
					console.log(thm);
					$("#pingjia").html(thm);
					$('#comtent').text("用户评价("+i+")");
				}
			})	
		}
	})
}
function notice(id){
	//var id = 121;
	$.ajax({
		//"url" : "http://127.0.0.1/app/ins/islookNotice",
		"url" : Server + "/app/ins/islookNotice",
		"type" : "Post",
		"dataType" : "Json",
		"data" : {
			iiId:id
		},
		success : function(data){
			var Time1 = "";
			var Time2 = ""
			if(data.startTime!=null && data.startTime!='' && data.startTime!= undefined){
				Time1 =( data.startTime).substring(0,( data.startTime).indexOf(' '));
			}
			if(data.endTime!=null && data.endTime!='' && data.endTime!= undefined){
				Time2 =( data.endTime).substring(0,( data.endTime).indexOf(' '));
			}
			$("#noticeContent").html(data.content);
			$("#time1").html(Time1);
			$("#time2").html(Time2);
		}
		
	})
}
//查找已被预订的日期时间
function unavailable(id){
	$.ajax({
		url: Server + "/app/time/unavailable",
		"type" : "Post",
		data: {
			id: id
		},
		success: function(data){
			localData.unavailable = data;
			initCalendar();
		},
		error: function(error){
			console.log(error)
		}
	})
}
function timeSolt(id){
	$.ajax({
		url: Server+"/app/time/timeslot",
		data: {
			id: id
		},
		success: function(data){
			console.log(data)
			localData.week = data 
			localData.money = data
			initOption()
			unavailable(id)
		},
		error: function(error){
			console.log(error)
		}
	})
}
//初始化时间段
function initOption(){
	
	var htmlStr = ""
	localData.week.forEach(item=>{
		htmlStr += "<option value='"+item.timeId+"'>"+item.slotStart+"~"+item.slotEnd+"</option>" 
	})
	$('#timeSlot').html(htmlStr)
}
//时间选择改变事件，判断是否在设置的时间内 && 设置价格
function dateChange(){
	var sdate = new Date($('#startDate').val()+" 00:00:00")
	var edate = new Date($('#endDate').val()+" 00:00:00")
	if(sdate.getTime()- new Date().getTime() < -24*60*60*1000){
		$("#deteErrorHint").html('开始时间早于今天');
		$("#deteErrorHint").show();
		return;
	}
	if(sdate < new Date(localData.money[0].planStart) || edate > new Date(localData.money[0].planEnd)){
		$("#deteErrorHint").html('所选时间不在超出或不在设置的时间段内');
		$("#deteErrorHint").show();
		return;
	}
	if(sdate>edate){
		$("#deteErrorHint").html('结束日期不能在开始日期前！');
		$("#deteErrorHint").show();
		return;
	}
	var dayNum = 0//(edate-sdate)/(24*60*60*1000)
	var timeID = $('#timeSlot').val()
	while(sdate<=edate){
		if(!canUse(sdate,timeID)){
			dayNum++
		}
		sdate = new Date(sdate.getTime()+24*60*60*1000)
	}
	if($.trim(localData.money[0].unit)==='元/小时'){
		var time = getTimeSolt(timeID)
		dayNum = dayNum*time
	}
	$('#computedPrice').html(dayNum*localData.money[0].total)
	
	
	$("#deteErrorHint").hide();
	//console.log()
}
//初始化日历控件
function initCalendar(){
	var sDate = new Date(localData.week[0].planStart)
	var eDate = new Date(localData.week[0].planEnd)
	//var events = []
	localData.events = []
	while(sDate<=eDate){
		// localData.events = localData.events.concat(isCanPre(sDate))
		sDate = new Date(sDate.getTime()+24*60*60*1000)
	}
	for(var i=0; i < localData.unavailable.length; i++){
		sDate = new Date(localData.unavailable[i].planStart);
		eDate = new Date(localData.unavailable[i].planEnd);
		localData.week = localData.unavailable.slice(i,i+1);//不应该是循环所有的而是循环预约的 它本身
		while(sDate<=eDate){
			localData.events = localData.events.concat(isCanPre(sDate))
			sDate = new Date(sDate.getTime()+24*60*60*1000)
		}
	}
	console.log(localData.events)
	 $('#calendar').fullCalendar({
         header: {
             left: 'prev,next today',
             center: 'title',
             right: 'month,agendaWeek,agendaDay,listMonth'
         },
        
         defaultDate: new Date(),
         locale: 'zh-cn',
         buttonIcons: false, // show the prev/next text
         weekNumbers: true,
         navLinks: false, // can click day/week names to navigate views
         editable: false,
         eventLimit: 1, // allow "more" link when too many events
         events: localData.events
     });
}
function isCanPre(date){
	var week = date.getDay();  
	switch (week) {  
	        case 0 :  
	            return isOpen('sun',date)  
	        case 1 :  
	        	return isOpen('mon',date)  
	        case 2 :  
	        	return isOpen('tue',date)  
	        case 3 :  
	        	return isOpen('wed',date)  
	        case 4 :  
	        	return isOpen('thur',date)   
	        case 5 :  
	        	return isOpen('fri',date)   
	        case 6 :  
	        	return isOpen('sat',date)   
	}  
}
//判断周？是否有开放
function isOpen(week,date){
	var arr = []
	localData.week.forEach(item=>{
		if(item[week]==0 && isExist(arr,date,item.timeId)){
			arr.push({
				title: item.slotStart+'-'+item.slotEnd+' 不可用',
	            start: date,
	            end: date,
	            block: true,
	            id: item.timeId
			})
		}
	})
	return arr
}
function isExist(arr,date,id){
	var temp = localData.events.filter(item=>{
		return item.id==id && item.start.getTime() == date.getTime()
	})
	return temp.length == 0
}
//判断该天该时间段是否可用
function canUse(date,id){
	var arr = localData.events.filter(item=>{
		return item.start.getTime()==date.getTime() && id ==item.id
	})
	//console.log(arr)
	return arr.length>0
}
//按小时算钱获取间隔时间
function getTimeSolt(id){
	
	var arr = localData.money.filter(item=>{
		return item.timeId == id
	})
	
	return arr.length!=0?parseInt(arr[0].slotEnd)-parseInt(arr[0].slotStart):0
}